package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.NewsDetail;
import com.qf.mapper.NewsCommentMapper;
import com.qf.mapper.NewsDetailMapper;
import com.qf.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/2
 */
@Service
public class NewsDetailServiceImpl implements NewsDetailService {
    @Autowired
    private NewsDetailMapper newsDetailMapper;

    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    public PageInfo<NewsDetail> selectByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //这里练习一对多，并且查询结果按查询结果降序排列
        List<NewsDetail> list = newsDetailMapper.selectByPage();
        PageInfo<NewsDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<NewsDetail> selectByNewsTitle(Integer pageNum,Integer pageSize,String titleName) {
        PageHelper.startPage(pageNum,pageSize);
        List<NewsDetail> list = newsDetailMapper.selectByNewsTitle("%"+titleName+"%");
        PageInfo<NewsDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public NewsDetail selectByNId(Integer nid) {
        return newsDetailMapper.selectByNid(nid);
    }

    @Override
    @Transactional
    public void removeNews(Integer nid) {
        newsDetailMapper.deleteByPrimaryKey(nid);
        newsCommentMapper.deleteByNid(nid);
    }
}
